package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.mapper.DepartmentMapper;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public void saveOrUpdate(Department department) {
        if(department.getId()!=null){
            departmentMapper.updateByPrimaryKey(department);
        }else {
            departmentMapper.insert(department);
        }
    }

    @Override
    public Department get(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Department> selectAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {

        int count = departmentMapper.queryForCount(qo);
        PageResult pageResult = null;
        if(count == 0){
            pageResult = new PageResult(count, Collections.EMPTY_LIST);
        }
        List<Department> list = departmentMapper.queryForList(qo);
        return new PageResult(count,list);
    }

    @Override
    public void updateState(Long id) {
        departmentMapper.updateState(id);
    }
}
